package com.yxhl.zoume.core.func.passenger.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.yxhl.protobuf.ScheduleType;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.core.func.passenger.event.DeletePsgerEvent;
import com.yxhl.zoume.core.func.passenger.event.SelectPsgerEvent;
import com.yxhl.zoume.core.func.passenger.event.SelectTakerEvent;
import com.yxhl.zoume.data.http.model.passenger.ZMPassenger;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.utils.IDNoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Xboy on 16/4/24.
 */
public class SelectPsgersAdapter extends BaseAdapter<SelectPsgersAdapter.SelectPsgersViewHolder> {

    private Context context;
    private List<ZMPassenger> passengers;
    private PsgerItemType itemType;

    private ScheduleType mScheduleType;

    public SelectPsgersAdapter(Context context, List<ZMPassenger> passengers, PsgerItemType itemType) {
        this.context = context;
        this.passengers = passengers;
        this.itemType = itemType;
    }

    public SelectPsgersAdapter(Context context, List<ZMPassenger> passengers, PsgerItemType itemType, ScheduleType mScheduleType) {
        this.context = context;
        this.passengers = passengers;
        this.itemType = itemType;
        this.mScheduleType = mScheduleType;
    }

    @Override
    public SelectPsgersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.item_recycler_passenger, parent, false);
        return new SelectPsgersViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(SelectPsgersViewHolder holder, int position) {
        holder.onBindPassenger(passengers.get(position));
    }

    @Override
    public int getItemCount() {
        if (null != passengers) {
            return passengers.size();
        }
        return 0;
    }

    public class SelectPsgersViewHolder extends BaseAdapter.BaseViewHolder {

        @BindView(R.id.rl_item_psger_info_container)
        RelativeLayout psgerInfoContainerRl;
        // CheckBox
        @BindView(R.id.psger_info_checkbox)
        CheckBox mPsgerSelectCheckBox;
        // 乘客姓名
        @BindView(R.id.psger_info_name_textview)
        TextView mPsgerNameTextView;
        // 身份证号码
        @BindView(R.id.psger_info_id_textview)
        TextView mPsgerIdTextView;
        // 乘客类别
        @BindView(R.id.psger_info_category_textview)
        TextView mPsgerCategoryTextView;

        public SelectPsgersViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            // 根据item类型配置item
            switch (itemType) {
                case NORMAL:
                    mPsgerSelectCheckBox.setVisibility(View.GONE);
                    break;
                case MULTI_SELECT:
                    mPsgerSelectCheckBox.setButtonDrawable(R.drawable.passenger_state_selector);
                    break;
                case SINGLE_SELECT:
                    mPsgerSelectCheckBox.setButtonDrawable(R.drawable.passenger_state_selector);
                    break;
                case DELETABLE:
                    mPsgerSelectCheckBox.setButtonDrawable(R.drawable.ic_delete);
                    mPsgerSelectCheckBox.setEnabled(true);
                    mPsgerSelectCheckBox.setClickable(true);
                    break;
            }
        }

        @OnClick(R.id.rl_item_psger_info_container)
        void onItemClick() {
            ZMPassenger p = passengers.get(getLayoutPosition());
            switch (itemType) {
                case MULTI_SELECT:
                    handleMultiSelectClick(p);
                    break;
                case SINGLE_SELECT:
                    handleSingleSelectClick(p);
                    break;
            }
        }

        @OnClick(R.id.psger_info_checkbox)
        void onCheckBoxClick() {
            // 删除
            ZMPassenger zmPassenger = passengers.get(getLayoutPosition());
            switch (itemType) {
                case MULTI_SELECT:
                    handleMultiSelectClick(zmPassenger);
                    break;
                case SINGLE_SELECT:
                    handleSingleCheckout(zmPassenger);
                    break;
                case DELETABLE:
                    if (mScheduleType != null) {
                        switch (mScheduleType) {
                            case V_DIY_BUS:
                            case V_FT_BUS:
                                if (ZMPassenger.ADULT == zmPassenger.getPsgerCategory()) {
                                    if (!isAdultCountMoreThanChildCount()) {
                                        return;
                                    }

                                }
                                break;
                            default:
                        }

                        RxBus.getInstance().send(new DeletePsgerEvent(zmPassenger));
                        notifyItemRemoved(getLayoutPosition());

                    } else if (isAdultCountMoreThanChildCount()) {
                        RxBus.getInstance().send(new DeletePsgerEvent(zmPassenger));
                        notifyItemRemoved(getLayoutPosition());
                    }

                    break;
            }
        }

        public void onBindPassenger(ZMPassenger passenger) {
            mPsgerSelectCheckBox.setChecked(passenger.isSelected());

            mPsgerNameTextView.setText(passenger.getRealName());

            mPsgerIdTextView.setText(IDNoUtils.encryptIDNo(passenger.getIdNumber()));

            String psgerCategory;
            if (ZMPassenger.ADULT == passenger.getPsgerCategory()) {
                psgerCategory = context.getString(R.string.adult);
            } else {
                psgerCategory = context.getString(R.string.child);
            }
            mPsgerCategoryTextView.setText(psgerCategory);
        }

        /**
         * 多选的点击处理(e.g.选择乘车人)
         * by Xboy
         *
         * @param p
         */
        private void handleMultiSelectClick(ZMPassenger p) {
            p.setSelected(!p.isSelected());
            notifyDataSetChanged();
            RxBus.getInstance().send(new SelectPsgerEvent(p));
        }

        /**
         * 单选的点击处理(e.g.选择取票人)
         * by Xboy
         *
         * @param p
         */
        private void handleSingleSelectClick(ZMPassenger p) {
            for (ZMPassenger ap : passengers) {
                if (ap.getPassengerId().equals(p.getPassengerId())) {
                    ap.setSelected(true);
                } else {
                    ap.setSelected(false);
                }
            }
            p.setSelected(true);
            notifyDataSetChanged();
            RxBus.getInstance().send(new SelectTakerEvent(p));
        }

        /**
         * checkout 单选
         *
         * @param p
         */
        private void handleSingleCheckout(ZMPassenger p) {
            for (ZMPassenger zmPassenger : passengers) {
                if (zmPassenger.getPassengerId().equals(p.getPassengerId())) {
                    zmPassenger.setSelected(true);
                } else {
                    zmPassenger.setSelected(false);
                }
            }
            notifyDataSetChanged();
            RxBus.getInstance().send(new SelectTakerEvent(p));
        }

        /**
         * 计算成人、儿童数
         * 用于判断成人和儿童人数配比是否合理
         */
        private boolean isAdultCountMoreThanChildCount() {
            int adultCount = 0;
            int childCount = 0;
            for (ZMPassenger p : passengers) {
                if (ZMPassenger.ADULT == p.getPsgerCategory()) {
                    ++adultCount;
                } else {
                    ++childCount;
                }
            }
            if (adultCount <= childCount) {
                new MaterialDialog.Builder(context)
                        .title(R.string.dialog_title_hint)
                        .content(R.string.child_cannot_more_than_adult)
                        .positiveText(R.string.dialog_confirm)
                        .positiveColorRes(R.color.app_primary_blue)
                        .canceledOnTouchOutside(false)
                        .show();
                return false;
            }
            return true;
        }
    }
}
